﻿using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;

namespace Jack.RedisEventBus
{
    /// <summary>
    /// 事件数据
    /// </summary>
    public class EventData
    {
        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; }
        /// <summary>
        /// 事件数据
        /// </summary>
        public string Data { get; set; }
        /// <summary>
        /// 投递次数
        /// </summary>
        public int DeliveryTimes { get; set; }

        public T ConvertData<T>()
        {
            return JsonConvert.DeserializeObject<T>(Data);
        }

        public static explicit operator EventData(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return default;
            return JsonConvert.DeserializeObject<EventData>(value);
        }

        public static explicit operator string(EventData data)
        {
            return JsonConvert.SerializeObject(data);
        }
    }
}
